home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / quicktime / importers and exporters / carbon qt graphic import / source / graphicimportdrawcode.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  13.7 KB  |  474 lines

  1. /*
  2.     File:        GraphicImportDrawCode.c
  3.     
  4.     Description:Code that actually implements the drawing calls to the Graphic Importer
  5.                 components to draw many different image formats.
  6.  
  7.     Author:        MC
  8.  
  9.     Copyright:     © Copyright 1999-2000 Apple Computer, Inc. All rights reserved.
  10.     
  11.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  12.                 ("Apple") in consideration of your agreement to the following terms, and your
  13.                 use, installation, modification or redistribution of this Apple software
  14.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  15.                 please do not use, install, modify or redistribute this Apple software.
  16.  
  17.                 In consideration of your agreement to abide by the following terms, and subject
  18.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  19.                 copyrights in this original Apple software (the "Apple Software"), to use,
  20.                 reproduce, modify and redistribute the Apple Software, with or without
  21.                 modifications, in source and/or binary forms; provided that if you redistribute
  22.                 the Apple Software in its entirety and without modifications, you must retain
  23.                 this notice and the following text and disclaimers in all such redistributions of
  24.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  25.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  26.                 Apple Software without specific prior written permission from Apple.  Except as
  27.                 expressly stated in this notice, no other rights or licenses, express or implied,
  28.                 are granted by Apple herein, including but not limited to any patent rights that
  29.                 may be infringed by your derivative works or by other works in which the Apple
  30.                 Software may be incorporated.
  31.  
  32.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  33.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  34.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  35.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  36.                 COMBINATION WITH YOUR PRODUCTS.
  37.  
  38.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  39.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  40.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  41.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  42.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  43.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  44.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45.                 
  46.     Change History (most recent first):
  47.  
  48. */
  49.  
  50. #define TARGET_API_MAC_CARBON 1
  51.  
  52. #include <Windows.h>
  53. #include <Movies.h>
  54. #include <QuickTimeComponents.h>
  55. #include "GraphicImportDrawCode.h"
  56. #include "WindowCode.h"
  57. #include "LinkedList.h"
  58. #include "Structs.h"
  59. #include "Defines.h"
  60.  
  61. // Globals
  62. extern    PreferencesHandle            gPreferences;
  63. extern    long                        gNumGIComponents;
  64. extern    OSType *                    gAcceptedFlavorTypes;
  65.  
  66. static void MaxWindowSize (WindowPtr theWindow, Rect * maxBounds) {
  67.     WindowInfoHandle            winInfo;
  68.     SInt8                        hState;
  69.     Rect                        standard,
  70.                                 device,
  71.                                 global,
  72.                                 c,
  73.                                 s,
  74.                                 diffs;
  75.     RgnHandle                    tempRgn;
  76.     GDHandle                    theDevice;
  77.     CGrafPtr                    wport;
  78.     Point                        origin;
  79.  
  80.     if (theWindow != nil) {
  81.         winInfo = (WindowInfoHandle)GetWRefCon (theWindow);
  82.     }
  83.  
  84.     if (winInfo != nil && (**winInfo).sig == kMySig) {
  85.         hState = HGetState ((Handle)winInfo);
  86.         HLock ((Handle)winInfo);
  87.  
  88.         // make the window's port the current port
  89.         wport = GetWindowPort (theWindow);
  90.         SetPort (wport);
  91.         SetPt (&origin, 0, 0);
  92.         LocalToGlobal (&origin);
  93.  
  94.         // find the window's global coordinates
  95.         GetPortBounds (wport, &global);
  96.         OffsetRect (&global, origin.h, origin.v);
  97.  
  98.         // get the maximum intersecting screen
  99.         theDevice = GetMaxDevice (&global);
  100.         device = (**theDevice).gdRect;
  101.  
  102.         // if it's the main screen, adjust it for the menu bar height
  103.         if (theDevice == GetMainDevice ())
  104.             device.top += GetMBarHeight ();
  105.  
  106.         // calculate the difference between the window's content rectangle and its frame
  107.         tempRgn = NewRgn ();
  108.         GetWindowRegion (theWindow, kWindowContentRgn, tempRgn);
  109.         GetRegionBounds (tempRgn, &c);
  110.         GetWindowRegion (theWindow, kWindowStructureRgn, tempRgn);
  111.         GetRegionBounds (tempRgn, &s);
  112.         DisposeRgn (tempRgn);
  113.         SetRect (&diffs, (short)(c.left - s.left), (short)(c.top - s.top),
  114.                 (short)(s.right - c.right), (short)(s.bottom - c.bottom));
  115.  
  116.         // calculate the maximum bounds for the standard rectangle
  117.         SetRect (&standard, (short)(device.left + diffs.left), (short)(device.top + diffs.top),
  118.                 (short)(device.right - diffs.right), (short)(device.bottom - diffs.bottom));
  119.  
  120.         // Move the origin back to 0,0
  121.         OffsetRect (&standard, (short)-standard.left, (short)-standard.top);
  122.         ScaleImageToRect (&(**winInfo).winNatGraphicBoundsRect, &standard, maxBounds);
  123.         HSetState ((Handle)winInfo, hState);        
  124.     }
  125. }
  126.  
  127. static OSErr SizeGraphicWindow (WindowPtr theWindow) {
  128.     ComponentResult                err;
  129.     WindowInfoHandle            winInfo;
  130.     SInt16                        w, h;
  131.  
  132.     if (theWindow != nil) {
  133.         winInfo = (WindowInfoHandle)GetWRefCon (theWindow);
  134.     }
  135.  
  136.     if (winInfo != nil && (**winInfo).sig == kMySig) {
  137.         err = GraphicsImportGetBoundsRect ((**winInfo).winGraphicImporter, &(**winInfo).winNatGraphicBoundsRect);
  138.         if (err == noErr) {
  139.             MaxWindowSize (theWindow, &(**winInfo).winBounds);
  140.             w = (SInt16)((**winInfo).winBounds.right - (**winInfo).winBounds.left);
  141.             h = (SInt16)((**winInfo).winBounds.bottom - (**winInfo).winBounds.top);
  142.             SizeWindow (theWindow, w, h, false);
  143.         }
  144.     }
  145.  
  146.     return (OSErr)err;
  147. }
  148.  
  149. #define USEGWORLDS 1
  150.  
  151. static OSErr AllocateGWorld (WindowInfoHandle winInfo) {
  152.     OSErr                        err;
  153.  
  154.     (**winInfo).winGWorld = nil;
  155.  
  156. #if USEGWORLDS
  157.     err = NewGWorld (&((**winInfo).winGWorld), 32, &(**winInfo).winNatGraphicBoundsRect,
  158.                     nil, nil, pixPurge | useTempMem);
  159. #endif
  160.  
  161.     if ((**winInfo).winGWorld == nil) {
  162.         err = memFullErr;
  163.     }
  164.  
  165.     return err;
  166. }
  167.  
  168. static OSErr DoDraw (WindowInfoHandle winInfo) {
  169.     ComponentResult                err;
  170.     Boolean                        locked;
  171.     PixMapHandle                pixMap;
  172.  
  173.     err = AllocateGWorld (winInfo);
  174.     if (err == noErr) {
  175.         pixMap = GetGWorldPixMap ((**winInfo).winGWorld);
  176.  
  177.         if (LockPixels (pixMap)) {
  178.             locked = true;
  179.         } else {
  180.             locked = false;
  181.             // Memory is gone, so forget about using a GWorld
  182.             // use the current window set by the function calling this routine
  183.             DisposeGWorld ((**winInfo).winGWorld);
  184.             (**winInfo).winGWorld = nil;
  185.         }
  186.     } else if (err == memFullErr) {
  187.         err = noErr;
  188.     }
  189.  
  190.     if ((**winInfo).winGWorld != nil) {
  191.         err = GraphicsImportSetGWorld ((**winInfo).winGraphicImporter, (**winInfo).winGWorld, nil);
  192.     } else {
  193.         err = GraphicsImportSetBoundsRect ((**winInfo).winGraphicImporter, &(**winInfo).winBounds);
  194.     }
  195.  
  196.     if (err == noErr) {
  197.         (void)GraphicsImportSetQuality ((**winInfo).winGraphicImporter, (**gPreferences).quality);
  198.     }
  199.  
  200.     if (err == noErr) {
  201.         err = GraphicsImportDraw ((**winInfo).winGraphicImporter);
  202.     }
  203.  
  204.     if (locked == true) {
  205.         UnlockPixels (pixMap);
  206.     }
  207.  
  208.     return (OSErr)err;
  209. }
  210.  
  211. OSErr    FindAllGIComponents (void) {
  212.     Component                aComponent;
  213.     ComponentDescription    looking,
  214.                             cDesc;
  215.     OSErr                    err;
  216.  
  217.     looking.componentType = GraphicsImporterComponentType;
  218.     looking.componentSubType = 0;
  219.     looking.componentManufacturer = 0;
  220.     looking.componentFlags = 0;
  221.     looking.componentFlagsMask = 1 << 12;
  222.  
  223.     gNumGIComponents = CountComponents (&looking);
  224.     gAcceptedFlavorTypes = (OSType*)NewPtr ((long)(gNumGIComponents * sizeof (OSType)));
  225.     err = MemError ();
  226.  
  227.     if (err == noErr) {
  228.         long            i    = 0;
  229.  
  230.         aComponent = 0;        // start search from the top
  231.         do {
  232.             aComponent = FindNextComponent (aComponent, &looking);
  233.             if (aComponent != 0) {
  234.                 err = GetComponentInfo (aComponent, &cDesc, nil, nil, nil);
  235.                 if (err == noErr) {
  236.                     gAcceptedFlavorTypes[i++] = cDesc.componentSubType;
  237.                 }
  238.             }
  239.         } while (aComponent != 0 && err == noErr);
  240.     }
  241.  
  242.     return err;
  243. }
  244.  
  245. void ScaleImageToRect (Rect * origBounds, Rect * maxBounds, Rect * newBounds) {
  246.     double        scale;
  247.  
  248.     scale = 1.0;        // default is no scaling
  249.     newBounds->right = origBounds->right;
  250.     newBounds->left = origBounds->left;
  251.     newBounds->bottom = origBounds->bottom;
  252.     newBounds->top = origBounds->top;
  253.  
  254.     if ((newBounds->right - newBounds->left) > (maxBounds->right - maxBounds->left)) {
  255.         scale = (maxBounds->right - maxBounds->left) / (float)(newBounds->right - newBounds->left);
  256.         newBounds->right *= scale;
  257.         newBounds->left *= scale;
  258.         newBounds->bottom *= scale;
  259.         newBounds->top *= scale;
  260.     }
  261.  
  262.     if ((newBounds->bottom - newBounds->top) > (maxBounds->bottom - maxBounds->top)) {
  263.         scale = (maxBounds->bottom - maxBounds->top) / (float)(newBounds->bottom - newBounds->top);
  264.         newBounds->right *= scale;
  265.         newBounds->left *= scale;
  266.         newBounds->bottom *= scale;
  267.         newBounds->top *= scale;
  268.     }
  269.  
  270.     // Move the adjusted rect so that it fits in the original Rect.
  271.     // This is needed in the Nav Preview window.
  272.     newBounds->right += maxBounds->left;
  273.     newBounds->left += maxBounds->left;
  274.     newBounds->bottom += maxBounds->top;
  275.     newBounds->top += maxBounds->top;
  276. }
  277.  
  278. OSErr    NewWindowWithPict (FSSpec * theFileSpec, WindowPtr behindWindow) {
  279.     WindowPtr                    theWindow;
  280.     WindowInfoHandle            winInfo;
  281.     OSErr                        err;
  282.  
  283.     err = MakeNewWindow (theFileSpec->name, false, behindWindow, &theWindow);
  284.  
  285.     if (err == noErr) {
  286.         if (theWindow != nil) {
  287.             winInfo = (WindowInfoHandle)GetWRefCon (theWindow);
  288.         }
  289.  
  290.         if (winInfo != nil && (**winInfo).sig == kMySig) {
  291.             BlockMoveData (theFileSpec, &((**winInfo).winSpec), sizeof (FSSpec));
  292.         } else {
  293.             err = paramErr;
  294.         }
  295.     }
  296.  
  297.     if (err == noErr) {
  298.         CursHandle        cursor = nil;
  299.  
  300.         cursor = GetCursor (watchCursor);
  301.         SetCursor (*cursor);
  302.  
  303.         err = DrawFile (theWindow);
  304.  
  305.         ObscureCursor ();
  306.         cursor = (CursHandle)NewHandle (sizeof (Cursor));
  307.         GetQDGlobalsArrow (*cursor);
  308.         SetCursor (*cursor);
  309.         DisposeHandle ((Handle)cursor);
  310.     }
  311.  
  312.     return err;
  313. }
  314.  
  315. OSErr DrawHandle (WindowPtr theWindow) {
  316.     WindowInfoHandle            winInfo;
  317.     ComponentResult                err;
  318.     SInt8                        hState;                        
  319.     Rect                        winRect;
  320.  
  321.     if (theWindow != nil) {
  322.         winInfo = (WindowInfoHandle)GetWRefCon (theWindow);
  323.     }
  324.  
  325.     if (winInfo != nil && (**winInfo).sig == kMySig) {
  326.         hState = HGetState ((Handle)winInfo);
  327.         HLock ((Handle)winInfo);
  328.  
  329.         SetPort (GetWindowPort(theWindow));
  330.         (**winInfo).winGraphicImporter = OpenDefaultComponent (GraphicsImporterComponentType, (**winInfo).winGraphicType);
  331.  
  332.         if ((**winInfo).winGraphicImporter != nil) {
  333.             err = GraphicsImportSetDataHandle ((**winInfo).winGraphicImporter, (**winInfo).winDataHandle);
  334.         } else {
  335.             err = couldntGetRequiredComponent;
  336.         }
  337.  
  338.         if (err == noErr) {
  339.             SizeGraphicWindow (theWindow);
  340.             GetWindowPortBounds (theWindow, &winRect);
  341.         }
  342.  
  343.         if (err == noErr) {
  344.             err = DoDraw (winInfo);
  345.         }
  346.  
  347.         if (err == noErr) {
  348.             if ((**winInfo).winGWorld != nil) {
  349.                 // We drew into a GWorld, not the window, so an update event is needed
  350.                 InvalWindowRect (theWindow, &winRect);
  351.             }
  352.         }
  353.  
  354.         if ((**winInfo).winGraphicImporter != nil) {
  355.             (void)CloseComponent ((**winInfo).winGraphicImporter);
  356.             (**winInfo).winGraphicImporter = nil;
  357.         }
  358.  
  359.         HSetState ((Handle)winInfo, hState);
  360.  
  361.         if (err != noErr) {
  362.             ReleaseMemory (winInfo, true);
  363.         }
  364.     }
  365.  
  366.     return (OSErr)err;
  367. }
  368.  
  369. OSErr DrawFile (WindowPtr theWindow) {
  370.     WindowInfoHandle            winInfo;
  371.     ComponentResult                err;
  372.     SInt8                        hState;
  373.  
  374.     if (theWindow != nil) {
  375.         winInfo = (WindowInfoHandle)GetWRefCon (theWindow);
  376.     }
  377.  
  378.     if (winInfo != nil && (**winInfo).sig == kMySig) {
  379.         hState = HGetState ((Handle)winInfo);
  380.         HLock ((Handle)winInfo);
  381.  
  382.         SetPort (GetWindowPort(theWindow));
  383.         err = GetGraphicsImporterForFile (&(**winInfo).winSpec, &(**winInfo).winGraphicImporter);
  384.  
  385.         if (err == noErr) {
  386.             err = SizeGraphicWindow (theWindow);
  387.         }
  388.  
  389.         if (err == noErr) {
  390.             err = DoDraw (winInfo);
  391.         }
  392.  
  393.         // Generates the update event needed to get us to draw into the window.
  394.         ShowWindow (theWindow);
  395.  
  396.         if ((**winInfo).winGraphicImporter != nil) {
  397.             (void)CloseComponent ((**winInfo).winGraphicImporter);
  398.             (**winInfo).winGraphicImporter = nil;
  399.         }
  400.  
  401.         HSetState ((Handle)winInfo, hState);
  402.     }
  403.  
  404.     return (OSErr)err;
  405. }
  406.  
  407. OSErr UpdateGraphWindow (WindowPtr theWindow, RgnHandle updateRgn) {
  408. #pragma unused (updateRgn)
  409.     OSErr                        err;
  410.     WindowInfoHandle            winInfo;
  411.     SInt8                        hState;                        
  412.     Rect                        winBounds;
  413.     PixMapHandle                pixMap;
  414.  
  415.     if (theWindow != nil) {
  416.         winInfo = (WindowInfoHandle)GetWRefCon (theWindow);
  417.     }
  418.  
  419.     if (winInfo != nil && (**winInfo).sig == kMySig) {
  420.         hState = HGetState ((Handle)winInfo);
  421.         HLock ((Handle)winInfo);
  422.  
  423.         SetPort (GetWindowPort(theWindow));
  424.         if ((**winInfo).winMovie != nil) {
  425.             err = UpdateMovie ((**winInfo).winMovie);
  426.             MoviesTask (DoTheRightThing, DoTheRightThing);
  427.         } else {
  428.             if ((**winInfo).winGWorld != nil) {
  429.                 pixMap = GetGWorldPixMap ((**winInfo).winGWorld);
  430.  
  431.                 if (LockPixels (pixMap)) {
  432.                     GetWindowPortBounds (theWindow, &winBounds);
  433.                     CopyBits ((BitMap*)*GetPortPixMap ((**winInfo).winGWorld),
  434.                                 (BitMap*)*GetPortPixMap (GetWindowPort(theWindow)),
  435.                                 &(**winInfo).winNatGraphicBoundsRect, &winBounds, srcCopy, nil);
  436.                     UnlockPixels (pixMap);
  437.                     err = noErr;
  438.                 } else {
  439.                     DisposeGWorld ((**winInfo).winGWorld);
  440.                     (**winInfo).winGWorld = nil;
  441.  
  442.                     if ((**winInfo).winDataHandle != nil) {
  443.                         err = DrawHandle (theWindow);
  444.                     } else {
  445.                         err = DrawFile (theWindow);
  446.                     }
  447.  
  448.                     if ((**winInfo).winGWorld != nil) {
  449.                         pixMap = GetGWorldPixMap ((**winInfo).winGWorld);
  450.  
  451.                         if (LockPixels (pixMap)) {
  452.                             GetWindowPortBounds (theWindow, &winBounds);
  453.                             CopyBits ((BitMap*)*GetPortPixMap ((**winInfo).winGWorld),
  454.                                         (BitMap*)*GetPortPixMap (GetWindowPort(theWindow)),
  455.                                         &(**winInfo).winNatGraphicBoundsRect, &winBounds, srcCopy, nil);
  456.                             UnlockPixels (pixMap);
  457.                         }
  458.                     }
  459.                 }
  460.             } else {
  461.                 if ((**winInfo).winDataHandle != nil) {
  462.                     err = DrawHandle (theWindow);
  463.                 } else {
  464.                     err = DrawFile (theWindow);
  465.                 }
  466.             }
  467.         }
  468.  
  469.         HSetState ((Handle)winInfo, hState);
  470.     }
  471.  
  472.     return err;
  473. }
  474.